<?php
session_start();
//-------
// Includes
if(file_exists('functions.inc.php'))
	include_once('functions.inc.php');
elseif(file_exists('../functions.inc.php'))
	include_once('../functions.inc.php');
else
{
	throw new Exception('Error : Cannot find ROOTDIR/functions.inc.php');
	die();
}

// Check if it's an ajax call -- if not, page manually loaded, exit
if(!isset($_POST) or empty($_POST) or !is_array($_POST) or empty($_POST['action']))
{
	echo('This page must not be loaded manually.');
	die();
}

// What do we do ?
switch($_POST['action'])
{
	// Show all or 1 note or a range of notes (pagination)
	case 'show':
		
		// show all
		if(!isset($_POST['param']) or empty($_POST['param']))
		{			
			// show a range of notes, stored in the session
			if(isset($_SESSION['start']) and isset($_SESSION['limit']) and $_SESSION['limit'] != 0) 
			{
				$start = $_SESSION['start'];
				$limit = $_SESSION['limit'];
				// Create a noteset with the notes
				$noteset = Note::load($start,$limit);
				// Create a pageset
				$bypage = 10;
				$pageset = new PageSet($noteset,$bypage);
				// Display notes
				foreach($noteset->getNotes() as $note)
					if(isset($_POST['view']) and $_POST['view'] == 'titles')
						include('../views/note_title.php');
					else
						include('../views/note_small.php');
				// Display pagination UI
				include_once('../views/pagination.php');
			}
			// no range of notes stored : show all notes
			else
			{
				$noteset = Note::load();
				foreach($noteset->getNotes() as $note)
					include('../views/note_small.php');
			}
		}
		// show a NEW range of notes
		elseif($_POST['param'] == 'range' and is_numeric($_POST['start']) and is_numeric($_POST['limit']))
		{	
			$start = $_SESSION['start'] = $_POST['start'];
			$limit = $_SESSION['limit'] = $_POST['limit'];
			
			// Create a noteset with the notes
			$noteset = Note::load($start,$limit);
			// Create a pageset
			$bypage = 10;
			$pageset = new PageSet($noteset,$bypage);
			
			// Display notes
			foreach($noteset->getNotes() as $note)
				if(isset($_POST['view']) and $_POST['view'] == 'titles')
					include('../views/note_title.php');
				else
					include('../views/note_small.php');
			// Display pagination UI
			include_once('../views/pagination.php');
		}
		// show one FULL note, identified by its id
		elseif($_POST['param'] == 'full' and isset($_POST['id']) and is_numeric($_POST['id']))
		{
			$note = Note::load($_POST['id']);
			include('../views/note_full.php');
		}
		// show one SMALL note, identified by its id
		elseif($_POST['param'] == 'small' and isset($_POST['id']) and is_numeric($_POST['id']))
		{
			$note = Note::load($_POST['id']);
			$note->display = false;
			if(isset($_POST['view']) and $_POST['view'] == 'titles')
				include('../views/note_title.php');
			else
				include('../views/note_small.php');
		}
		else
		{
			echo 'Error while loading note';
		}
		
	break;
		
	// New note
	case 'new':
		
		if(isset($_POST['title']) and isset($_POST['body']))
		{
			if(isset($_SESSION['user']))
			{
				$user = $_SESSION['user'];				
				$note = new Note('',$_POST['title'],$_POST['body'],$user);
				echo $note->create(); // send back to JS the id of this new record
			}
			else
				echo 'User unknown. Please logout and log back in.';
		}
		else
		{
			echo 'Title or body empty. Try again.';
		}
		
	break;
	
	// Update a single field
	case 'field_update':
	
		if(isset($_POST['id']) and isset($_POST['value']) and isset($_POST['field'])
			and is_numeric($_POST['id']))
		{
			// Save the field
			$note = Note::load($_POST['id']);
			$setField = 'set'.ucfirst($_POST['field']);
			$note->$setField($_POST['value']);
			$note->save($_POST['field']);
			
			// Get the new value (to make sure it has been saved...)
			// Avoidable server load, but data safety comes first
			$note = '';
			$note = Note::load($_POST['id']);
			$getField = 'get'.ucfirst($_POST['field']);
			$value = $note->$getField();
			echo $value;
		}
		else
		{
			throw new Exception('Error while updating field.');
		}
	
	break;
	
	default: break;
}

?>